from django.conf import settings

from EE.models import DecisionMill
from EE.models import StateMill
from EE.models import Windmill
from EE.logic import state_mill as state_mill_logic

def createNew(reference_decision, mill_array):
  arr = []
  for (number, mill) in mill_array:
    decision_mill = DecisionMill(decision=reference_decision, noOfMills3Months=number, typeOfMill=mill)
    decision_mill.noOfMills = 0
    decision_mill.noOfMaintenance = 0
    decision_mill.noOfBrokenMills = 0
    decision_mill.noOfMills1Month = 0
    decision_mill.noOfMills2Months = 0
    decision_mill.save()
    arr.append(decision_mill)
  return arr
  
def handle_made_turn(reference_decision, mill_arr):
  for (mill, number, maintenance) in mill_arr:
    ##print 'mill '+mill
    ##print 'number '+str(number)
    windmill = Windmill.objects.filter(name=mill).get()
    decision_mill = DecisionMill.objects.filter(decision=reference_decision).filter(typeOfMill=windmill).get()
    if reference_decision.game.round == 0:
	  decision_mill.noOfMills = decision_mill.noOfMills + number
    else:
      decision_mill.noOfMills3Months = number
    decision_mill.noOfMaintenance = maintenance
    decision_mill.save()
	
def handle_end_round(reference_decision):
  decision_mill = DecisionMill.objects.filter(decision=reference_decision).all()
  game_state = reference_decision.game
  for dm in decision_mill:
	state_mill = StateMill.objects.filter(state=game_state).filter(typeOfMill=dm.typeOfMill).get()
	state_mill.noOfMills = dm.noOfMills
	state_mill.noOfMills1Month = dm.noOfMills1Month
	state_mill.noOfMills2Months = dm.noOfMills2Months
	state_mill.noOfMills3Months = dm.noOfMills3Months
	state_mill.noOfMaintenance = dm.noOfMaintenance
	state_mill.noOfBrokenMills = dm.noOfBrokenMills
	state_mill.save()
	#dm.noOfMills = dm.noOfMills + dm.noOfMills1Month
	#dm.noOfMills1Month = dm.noOfMills2Months
	#dm.noOfMills2Months = dm.noOfMills3Months
	#dm.noOfMills3Months = 0
	#dm.save()
	
def updateDecisionMills(reference_decision, array):
  arr = []
  for (noOfMills, noOfMills1Month, noOfMills2Months, noOfMills3Months, noOfMaintenance, noOfBrokenMills, typeOfMill) in array:
    decision_mill = DecisionMill(decision=reference_decision)
    decision_mill.noOfMills = noOfMills+noOfMills1Month
    decision_mill.noOfMills1Month = noOfMills2Months
    decision_mill.noOfMills2Months = noOfMills3Months
    decision_mill.noOfMills3Months = 0
    decision_mill.noOfMaintenance = noOfMaintenance
    decision_mill.typeOfMill = typeOfMill
    decision_mill.noOfBrokenMills = 0
    decision_mill.save()
    arr.append(decision_mill)
  return arr
    
  
def getDecisionMill(reference_decision):
  if DecisionMill.objects.filter(decision=reference_decision).count() == 0:
    raise DecisionMill.DoesNotExist
  else:
    decision_mills = DecisionMill.objects.filter(decision=reference_decision)
    return decision_mills
	
def getTotalNoOfBrokenMills(reference_decision):
   mills = getDecisionMill(reference_decision)
   total = 0
   for m in mills:
     total = total + m.noOfBrokenMills
   return total
   
def handle_breakdown_mills(reference_decision, array):
   for (typeOfMill, numberOfBrokenMills) in array:
     dm = DecisionMill.objects.filter(decision=reference_decision).filter(typeOfMill=typeOfMill).get()
     dm.noOfBrokenMills = numberOfBrokenMills
     dm.save()